//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO;

import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.Iterator;

public abstract class PollIO {
    private static Selector iomap = null;
    private static final java.lang.Runnable task = new Task();
    private static Object regist_locker = new Object();
    protected SelectableChannel channel;

    static {
        try {
            iomap = Selector.open();
        } catch (Exception var1) {
            var1.printStackTrace();
        }

    }

    protected abstract int UpdateEvent();

    protected abstract boolean Close();

    protected void PollIn() {
    }

    protected void PollOut() {
    }

    protected void PollAccept() {
    }

    protected void PollConnect() {
    }

    public static PollIO register(PollIO io) {
        synchronized(regist_locker) {
            WakeUp();

            try {
                io.channel.register(iomap, 0, io);
            } catch (ClosedChannelException var3) {
                var3.printStackTrace();
            }

            return io;
        }
    }

    protected static synchronized void Poll(long timeout) {
        try {
            ArrayList<SelectionKey> al = new ArrayList();
            synchronized(regist_locker) {
                Iterator<SelectionKey> it = iomap.keys().iterator();

                while(true) {
                    if (!it.hasNext()) {
                        break;
                    }

                    al.add((SelectionKey)it.next());
                }
            }

            Iterator<SelectionKey> it = al.iterator();

            PollIO io;
            SelectionKey sk;
            while(it.hasNext()) {
                sk = (SelectionKey)it.next();
                io = (PollIO)sk.attachment();
                int event = io.UpdateEvent();
                if (event == -1) {
                    if (io.Close()) {
                        try {
                            io.channel.close();
                        } catch (Exception var9) {
                        }

                        sk.cancel();
                    }
                } else {
                    try {
                        sk.interestOps(event);
                    } catch (Exception var8) {
                    }
                }
            }

            iomap.selectedKeys().clear();
            if (timeout < 0L) {
                iomap.select();
            } else if (timeout == 0L) {
                iomap.selectNow();
            } else {
                iomap.select(timeout);
            }

            it = iomap.selectedKeys().iterator();

            while(it.hasNext()) {
                sk = (SelectionKey)it.next();
                io = (PollIO)sk.attachment();
                if (sk.isAcceptable()) {
                    io.PollAccept();
                }

                if (sk.isConnectable()) {
                    io.PollConnect();
                }

                if (sk.isReadable()) {
                    io.PollIn();
                }

                if (sk.isWritable()) {
                    io.PollOut();
                }
            }
        } catch (Exception var11) {
            var11.printStackTrace();
        }

    }

    protected PollIO(SelectableChannel sc) {
        try {
            this.channel = sc;
            sc.configureBlocking(false);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    public static Runnable GetTask() {
        return task;
    }

    public static void WakeUp() {
        iomap.wakeup();
    }
}
